home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr48 / sweep10.zip / STRING.CPP < prev    next >
Text File  |  1993-04-01  |  3KB  |  129 lines

  1. //////////////////
  2. // String Class //
  3. //////////////////
  4.  
  5. #include <string.h>
  6. #include <iostream.h>
  7.  
  8. #include "string.hpp"
  9.  
  10. ////////////////////
  11. // Create string. //
  12. ////////////////////
  13.  
  14. String::String()
  15. {
  16.   han = HeapAlloc (1);          // allocate handle
  17.   *(Address()) = 0;             // store nul
  18. }
  19.  
  20. String::String (char *s)
  21. {
  22.   han = HeapAlloc (0);          // allocate block
  23.   Assign (s);                   // assign string
  24. }
  25.  
  26. String::String (String& s)
  27. {
  28.   han = HeapAlloc (0);          // allocate block
  29.   Assign (s);                   // assign string
  30. }
  31.  
  32. ///////////////////////
  33. // Terminate string. //
  34. ///////////////////////
  35.  
  36. String::~String ()
  37. {
  38.   HeapFree (han);               // release heap memory
  39. }
  40.  
  41. ////////////////////////////
  42. // Assign a string value. //
  43. ////////////////////////////
  44.  
  45. String& String::Assign (char * s)
  46. {
  47.   unsigned l = strlen (s) + 1;                  // length of string
  48.   HeapResize (han, l);                          // resize block
  49.   CopyForward (HeapAddr (han), s, l);           // copy data
  50.   return *this;
  51. }
  52.  
  53. String& String::Assign (String& s)
  54. {
  55.   unsigned l = s.Length() + 1;                  // length of string
  56.   HeapResize (han, l);                          // resize block
  57.   CopyForward (HeapAddr(han), s.Address(), l);  // copy data
  58.   return *this;
  59. }
  60.  
  61. ///////////////////////////////////
  62. // Insert a string to the front. //
  63. ///////////////////////////////////
  64.  
  65. String& String::Prefix (char *s)
  66. {
  67.   unsigned l1 = strlen(s);              // bytes to add
  68.   unsigned l2 = Length() + 1;           // current bytes
  69.   HeapResize (han, l1 + l2);            // resize block
  70.   char far *p = Address();              // current address
  71.   CopyBackward (p + l1, p, l2);         // make room for new string
  72.   CopyForward (p, s, l1);               // copy new string
  73.   return *this;
  74. }
  75.  
  76. String& String::Prefix (String& s)
  77. {
  78.   unsigned l1 = s.Length();             // bytes to add
  79.   unsigned l2 = Length() + 1;           // current bytes
  80.   HeapResize (han, l1 + l2);            // resize block
  81.   char far *p = Address();              // current address
  82.   CopyBackward (p + l1, p, l2);         // make room for new string
  83.   CopyForward (p, s.Address(), l1);     // copy new string
  84.   return *this;
  85. }
  86.  
  87. //////////////////////////////////
  88. // Insert a string to the back. //
  89. //////////////////////////////////
  90.  
  91. String& String::Suffix (char *s)
  92. {
  93.   unsigned l1 = strlen(s) + 1;          // bytes to add
  94.   unsigned l2 = Length();               // current bytes
  95.   HeapResize (han, l1 + l2);            // resize block
  96.   CopyForward (Address() + l2, s, l1);  // copy new string
  97.   return *this;
  98. }
  99.  
  100. String& String::Suffix (String& s)
  101. {
  102.   unsigned l1 = s.Length() + 1;                 // bytes to add
  103.   unsigned l2 = Length();                       // current bytes
  104.   HeapResize (han, l1 + l2);                    // resize block
  105.   CopyForward (Address()+l2, s.Address(), l1);  // copy new string
  106.   return *this;
  107. }
  108.  
  109. ////////////////////////////
  110. // Redirection functions. //
  111. ////////////////////////////
  112.  
  113. char* operator<< (char* s1, String& s2)
  114. {
  115.   CopyForward (s1, s2.Address(), s2.Length() + 1);
  116.   return s1;
  117. }
  118.  
  119. String& operator<< (String& s1, char* s2)
  120. {
  121.   return s1.Assign (s2);
  122. }
  123.  
  124. String& operator<< (String& s1, String& s2)
  125. {
  126.   return s1.Assign (s2);
  127. }
  128.  
  129.